package com.net.wanjian.networkhospitalmanager.activity.dopsminicex;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.ScanPortraitActivity;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.bean.dopsminicex.NumberGetUserBean;
import com.net.wanjian.networkhospitalmanager.bean.dopsminicex.StudentInformationBean;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;

import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

public class DopsBaseInfoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.lab_details_enter_btn)
    Button labDetailsEnterBtn;
    @BindView(R.id.scan_QrCode_iv)
    ImageView scanQrCodeIv;
    @BindView(R.id.edit_UserInfoCode)
    EditText editUserInfoCode;
    @BindView(R.id.tv_UserInfoTrueName)
    TextView tvUserInfoTrueName;
    @BindView(R.id.tv_RoleName)
    TextView tvRoleName;
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_back_text_tv)
    TextView topBackTextTv;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.img_dops_UserInfoCode)
    ImageView imgDopsUserInfoCode;
    @BindView(R.id.checkbox_first)
    CheckBox checkboxFirst;
    @BindView(R.id.checkbox_repeat)
    CheckBox checkboxRepeat;
    @BindView(R.id.checkbox_man)
    CheckBox checkboxMan;
    @BindView(R.id.checkbox_woman)
    CheckBox checkboxWoman;
    @BindView(R.id.edit_address_dops)
    EditText editAddressDops;
    @BindView(R.id.edit_age_dops)
    EditText editAgeDops;
    @BindView(R.id.ll_dops_first)
    LinearLayout llDopsFirst;
    @BindView(R.id.ll_dops_repeat)
    LinearLayout llDopsRepeat;
    @BindView(R.id.ll_dops_man)
    LinearLayout llDopsMan;
    @BindView(R.id.ll_dops_woman)
    LinearLayout llDopsWoman;
    private String UserIdentityID;
    private String EvaluationRecordOutCallType = "初诊";
    private String EvaluationRecordPatientGender = "男";

    @Override
    protected void initData() {
        initEvents();

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_dops_base_info;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    private void initEvents() {
        NoClick();
        labDetailsEnterBtn.setOnClickListener(this);
        scanQrCodeIv.setOnClickListener(this);
        imgDopsUserInfoCode.setOnClickListener(this);
        topBackLayout.setOnClickListener(this);
        llDopsFirst.setOnClickListener(this);
        llDopsRepeat.setOnClickListener(this);
        llDopsMan.setOnClickListener(this);
        llDopsWoman.setOnClickListener(this);

    }

    private void NoClick() {
        checkboxFirst.setEnabled(false);
        checkboxRepeat.setEnabled(false);
        checkboxMan.setEnabled(false);
        checkboxWoman.setEnabled(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lab_details_enter_btn:
                Intent intent = new Intent(DopsBaseInfoActivity.this, DopsScoreActivity.class);
                intent.putExtra("UserIdentityID", UserIdentityID);
                intent.putExtra("EvaluationRecordLocation", editAddressDops.getText().toString().trim());
                intent.putExtra("EvaluationRecordOutCallType", EvaluationRecordOutCallType);
                intent.putExtra("EvaluationRecordPatientGender", EvaluationRecordPatientGender);
                intent.putExtra("EvaluationRecordPatientAge", editAgeDops.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.scan_QrCode_iv:
                scanQrCode();
                break;
            case R.id.top_back_layout:
                finish();
                break;
            case R.id.img_dops_UserInfoCode:
                InRequestNetwork();
                break;
            case R.id.ll_dops_first:
                checkboxFirst.setChecked(true);
                checkboxRepeat.setChecked(false);
                EvaluationRecordOutCallType = "初诊";
                break;
            case R.id.ll_dops_repeat:
                checkboxRepeat.setChecked(true);
                checkboxFirst.setChecked(false);
                EvaluationRecordOutCallType = "复诊";
                break;
            case R.id.ll_dops_man:
                checkboxMan.setChecked(true);
                checkboxWoman.setChecked(false);
                EvaluationRecordPatientGender = "男";
                break;
            case R.id.ll_dops_woman:
                checkboxWoman.setChecked(true);
                checkboxMan.setChecked(false);
                EvaluationRecordPatientGender = "女";
                break;
        }
    }

    private void InRequestNetwork() {
        String trim = editUserInfoCode.getText().toString().trim();
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.NumberGetUser(xmlUtil.getHospitalId(), xmlUtil.getDeviceId(), xmlUtil.getToken(), trim,
                new DisposableObserver<NumberGetUserBean>() {
                    @Override
                    public void onNext(NumberGetUserBean numberGetUserBean) {
                        if (numberGetUserBean.getResult() == 1) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(numberGetUserBean.getData().getToken()));
                            NumberGetUserBean.DataBean.UserInfoBean userInfo = numberGetUserBean.getData().getUserInfo();
                            String userInfoTrueName = URLDecoderUtil.getDecoder(userInfo.getUserInfoTrueName());
                            List<NumberGetUserBean.DataBean.UserInfoBean.UserIdentityListBean> userIdentityList = userInfo.getUserIdentityList();
                            String roleName = URLDecoderUtil.getDecoder(userIdentityList.get(0).getRoleName());
                            //用户身份ID
                            String decoder = URLDecoderUtil.getDecoder(userIdentityList.get(0).getUserIdentityID());
                            UserIdentityID = decoder;
                            tvUserInfoTrueName.setText(userInfoTrueName);
                            tvRoleName.setText(roleName);

                        } else {
                            ToastUtil.showToast("输入工号有误");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void scanQrCode() {
        IntentIntegrator integrator = new IntentIntegrator(DopsBaseInfoActivity.this);
        integrator.setPrompt("请扫描二维码");
        integrator.setCaptureActivity(ScanPortraitActivity.class);
        integrator.initiateScan();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

        if (result != null) {
            if (result.getContents() == null || !result.getContents().contains("WANJIAN^USER^")) {
                //弹出提示
                ToastUtil.showToast("扫描失败,请重新扫描");
            } else {
                String fromBase = result.getContents().split("\\^")[2];
                String decoder = URLDecoderUtil.getDecoder(fromBase);
                SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
                HttpUtil.ScanUserQRcode(xmlUtil.getHospitalId(), xmlUtil.getDeviceId(), xmlUtil.getToken(), decoder,
                        new DisposableObserver<StudentInformationBean>() {
                            @Override
                            public void onNext(StudentInformationBean studentInformationBean) {
                                if (studentInformationBean.getResult() == 1) {
                                    SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(studentInformationBean.getData().getToken()));
                                    StudentInformationBean.DataBean.UserIdentityBean userIdentity = studentInformationBean.getData().getUserIdentity();
                                    String decoder1 = userIdentity.getUserIdentityID();
                                    UserIdentityID = decoder1;
                                    String userInfoCode = URLDecoderUtil.getDecoder(userIdentity.getUserInfoCode());
                                    String roleName = URLDecoderUtil.getDecoder(userIdentity.getRoleName());
                                    String userInfoTrueName = URLDecoderUtil.getDecoder(userIdentity.getUserInfoTrueName());
                                    editUserInfoCode.setText(userInfoCode);
                                    tvUserInfoTrueName.setText(userInfoTrueName);
                                    tvRoleName.setText(roleName);

                                } else {
                                    ToastUtil.showToast("获取用户信息失败");
                                }

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        }
    }

}
